package com.hwhp.service.console.impl;

import java.util.ArrayList;
import java.util.List;

import com.hwhp.common.Pager;
import com.hwhp.dao.HibernateBaseDao;
import com.hwhp.dao.HibernateUtil;
import com.hwhp.entity.YzUser;
import com.hwhp.service.console.UserService;


public class UserServiceImpl implements UserService {
	private HibernateBaseDao baseDao = null ;
	
	/**
	 * 
	 * @param hql
	 * @return
	 */
	public  List queryUserByDeptId(String DeptId){
		
		List<YzUser> userList = new ArrayList();
		if(DeptId==null||"".equals(DeptId)){
			DeptId="-1";
		}
		
		System.out.println("queryUserbydeptId :"+DeptId);
		
		String hql="from YzUser where deptId='"+DeptId+"'";
		
		
		userList = HibernateUtil.query(hql);
		
		//userList = baseDao.query(hql);
		return userList;
	}
	/**
	 * 
	 * @param u
	 * @return
	 */
	public  boolean insertUser(YzUser u){
		boolean success = false ;
		try{
    		//HibernateUtil.save(u);
			baseDao.save(u);
    		success = true ;
    	}catch(Exception e){
    		success = false ;
    	}
		return success ;
	}
	/**
	 * 
	 * @param u
	 * @return
	 */
	public  boolean editUser(YzUser u){
		boolean success = false ;
		//success = HibernateUtil.update(u);
		try{
			baseDao.update(u);
			success = true ;
		}catch(Exception e){
			success = false ;
			e.printStackTrace();
		}
		return success ;
	}
	/**
	 * 
	 * @param u
	 * @return
	 */
	public  boolean deleteUser(YzUser u){
		boolean success = false ;
		//success = HibernateUtil.delete(u);
		try{
			baseDao.delete(u);
			success = true ;
		}catch(Exception e){
			success = false ;
		}
		return success ;
		
	}
	/**
	 * 
	 * @param userId
	 * @return
	 */
	public  YzUser getUser(String userId){
		YzUser u = null ;
		
		//u = (YzUser)HibernateUtil.get(YzUser.class, userId);
		
		u = (YzUser)baseDao.get(YzUser.class, userId);
	
		return u ;
	}
	
	public  String subDateStr(String dateStr){
		String tmpStr = dateStr.substring(0,10);
		return tmpStr;
	}
	public  List getUtors(String userId){
		String hql ="from User as u where u.userId='"+userId+"'";
		List utList = new ArrayList();
		//utList = HibernateUtil.query(hql);
		utList = baseDao.query(hql);
		
		return utList ;
	}
    
	public  List<YzUser> getUserListByIdSet(String userIdSet){
		List<YzUser> userList=new ArrayList();
		String hql="from YzUser as u where u.userId in ("+userIdSet+")";
		//userList = HibernateUtil.query(hql);
		userList = baseDao.query(hql);
		return userList;
	}

	public void setBaseDao(HibernateBaseDao baseDao) {
		this.baseDao = baseDao;
	}
	public HibernateBaseDao getBaseDao() {
		return baseDao;
	}
	public Pager getUserPagerByDeptId(String userName, String deptId,
			int pageNumber) {
		String hql = "from YzUser as u where u.deptId='"+deptId+"' and u.userName like'%"+userName+"%'";
		System.out.println(hql);
		Pager pager = new Pager();
		pager = baseDao.queryByPage(hql, pageNumber, 3);
		return pager;
	}

}
